package Test1;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;

import javax.swing.*;

public class GUI2 extends JFrame {

    private JButton first;
    private JButton prev;
    private JButton next;
    private JButton last;
    private JButton add;
    private JButton delete;
    private JButton save;
    private JButton search;
    public int index = 0;
    private List<String> lines;
    private int currentIndex;
    private JFrame textFrame;
    private JLabel label;
//    private JPanel panel;
    private javax.swing.JDialog panel;
    private JTextField brandtext;
    private JTextField unitstext;
    private JTextField pricetext;

    public GUI2() {
        super("Inventory Program Part 5");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        lines = new ArrayList<String>();
        setLayout(new FlowLayout());

        first = new JButton("First");
        add(first);

        prev = new JButton("Previous");
        add(prev);

        next = new JButton("Next");
        add(next);

        last = new JButton("Last");
        add(last);

        add = new JButton("Add");
        add(add);

        delete = new JButton("Delete");
        add(delete);

        save = new JButton("Save");
        add(save);

        search = new JButton("Search");
        add(search);

        ImageIcon icon = new ImageIcon(getClass().getResource("logoImage.JPG"));
//        ImageIcon icon = new ImageIcon(getClass().getResource("JK.JPG"));
        label = new JLabel(icon);
        add(label);

        HandlerClass handler = new HandlerClass();
        first.addActionListener(handler);
        prev.addActionListener(handler);
        next.addActionListener(handler);
        last.addActionListener(handler);

        addHandlerClass addHandler = new addHandlerClass();
        add.addActionListener(addHandler);
        delete.addActionListener(handler);
        save.addActionListener(handler);
        search.addActionListener(handler);
    }

    private ImageIcon createImageIcon(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    public GUI2(String arrayRecord) {
        this();

        try {
            BufferedReader reader = new BufferedReader(new FileReader(arrayRecord));
            String line = "";

            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void showFile() {
        for (int i = 0; i < lines.size(); i++) {
            System.out.println(lines.get(i));
        }
    }

    public static void main(final String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                public void run() {
//                    GUI2 f = new GUI2(args[0]);
                    GUI2 f = new GUI2();
                    f.setSize(200, 900);
                    f.setVisible(true);
                }
            });
        } catch (Exception e) {
            System.out.println("Error ar main run: " + e);
            System.out.println("Error ar main run: " + e.getCause());
        }
    }

    private class HandlerClass implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            textFrame = new JFrame("Inventory");
            textFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            textFrame.getContentPane();

            JTextArea textArea = new JTextArea();
            textFrame.add(textArea);

            Object button = event.getSource();

            if (first == button) {
                currentIndex = 0;
            } else if ((prev == button) && (currentIndex > 0)) {
                --currentIndex;
            } else if (next == button) {
                ++currentIndex;
            } else if (delete == button) {
            }

            // Wrap
            if (currentIndex == lines.size()) {
                currentIndex = 0;

            } else if (last == button) {
                currentIndex = lines.size() - 1;
            }

            textArea.setText(lines.get(currentIndex));
            textFrame.setSize(900, 150);
            textFrame.setVisible(true);
        }
    }

    private class addHandlerClass implements ActionListener {

        public void actionPerformed(ActionEvent event) {
//            panel = new JPanel();
            panel = new javax.swing.JDialog();  //new
            panel.setModal(true);//new
            panel.setLayout(new FlowLayout());  //new
            brandtext = new JTextField(10);
            unitstext = new JTextField(10);
            pricetext = new JTextField(10);
            panel.add(new JLabel("Enter Brand: "));
            panel.add(brandtext);
            panel.add(new JLabel("Enter Units:"));
            panel.add(unitstext);
            panel.add(new JLabel("Enter Price: "));
            panel.add(pricetext);
//            panel.setVisible(true);
            panel.setSize(200, 200);

            Object button = event.getSource();

            if (add == button) {
                panel.getContentPane();    //new
                panel.setVisible(true);  //new
//                <-<- <-probelem


                brandtext.setText("");
                unitstext.setText("");
                pricetext.setText("");
            }

        }
    }
}

